package bank;

import java.util.Scanner;
import java.util.InputMismatchException;
import java.util.Scanner;
import java.util.regex.Pattern;
/**
 *
 * @author tank
 */
public class bankSystem {

  Scanner scanner = new Scanner(System.in);
    BankAccount bank = new BankAccount();
    BankAccount[] accounts = new BankAccount[10];
    String AccountName;
    int AccountNumber;
    private double Amount;
   // private BankAccount [] Accounts;
    private int numberOfAccounts = 0;
    private ReaderWriter io = new ReaderWriter();

    public void RunApplication() {
       accounts = io.readFromFile();

         int b = -1;
        for (int i = 0; i < accounts.length; i++) {

            BankAccount BankAccount = accounts[i];
            if(BankAccount != null){
                b++;

            }
        }





        bank.Start();
        bank.Menu();
        int a = 0;

        boolean ok = true;
        while (ok) {

            a = scanner.nextInt();

            if (a == 1) {
                b++;
            }
            switch (a) {
                case 1:
                    bank.EnterAccountName();
                    AccountName = scanner.next();
                    bank.EnterAccountNumber();
                    AccountNumber = scanner.nextInt();
                    bank.EnterInitNumber();
                    Amount = scanner.nextInt();
                    accounts[b] = new BankAccount(AccountName, AccountNumber, Amount);

                    bank.Menu();
                    io.writeToFile(accounts);
                    break;


                case 2:
                    for (int n = 0; n < b + 1; n++) {
                        System.out.println((n + 1) + " Account Number: " + accounts[n].AccountNumber + " Account Holder: " + accounts[n].AccountName + " Amount: " + accounts[n].Amount);
                    }
                    bank.Transfer();
                    int choice = scanner.nextInt();
                    if (choice == 1) {
                        System.out.println("Select the account number you want to withdraw");
                        int e = scanner.nextInt();
                        for (int n = 0; n < b + 1; n++) {
                            if (e == accounts[n].AccountNumber) {
                                System.out.println("How much do you want to withdraw?");
                                int w = scanner.nextInt();
                                accounts[n].Amount = accounts[n].Amount - w;
                                if (accounts[n].Amount < 0) {
                                    System.out.println("This transaction is not allowed since the balance will be negative");
                                    accounts[n].Amount = accounts[n].Amount + w;
                                } else {
                                    System.out.println("Select the account number you want to deposit");
                                    int t = scanner.nextInt();
                                    for (int s = 0; s< b + 1; s++) {
                                        if (t == accounts[s].AccountNumber) {
                                            accounts[s].Amount = accounts[s].Amount + w;
                                        }
                                    }
                                }
                            }
                        }

                    } else if (choice == 2) {
                        System.out.println("You have chosen to deposit or withdraw from one account");
                        System.out.println("Select the account you want to deposit/withdraw");
                        int f = scanner.nextInt();
                        for (int i = 0; i < b + 1; i++) {
                            if (f == accounts[i].AccountNumber) {
                                System.out.println("You will deposit by entering a positive number");
                                System.out.println("You will withdraw by entering a negative number");
                                int g = scanner.nextInt();
                                accounts[i].Amount = accounts[i].Amount + g;
                                if (accounts[i].Amount < 0) {
                                    System.out.println("This transaction is not allowed since the balance will be negative");
                                    accounts[i].Amount = accounts[i].Amount - g;
                                }
                            }
                        }
                    }
                    bank.Menu();
                    break;
                case 3:
                    for (int n = 0; n < b + 1; n++) {

                        System.out.println((n + 1) + " Account Number: " + accounts[n].AccountNumber + " Account Holder: " + accounts[n].AccountName + " Amount: " + accounts[n].Amount);
                    }
                    System.out.println("Write the account Number you want to delete");
                    int x = scanner.nextInt();
                    for (int e = 0; e < b + 1; e++) {
                        if (accounts[e].AccountNumber == x) {
                            accounts[e] = null;
                            if(accounts[e]!=null)
                            {
                               System.out.println((e + 1) + " Account Number: " + accounts[e].AccountNumber + " Account Holder: " + accounts[e].AccountName + " Amount: " + accounts[e].Amount);
                            }
                        }



                    }
                    io.writeToFile(accounts);
                    bank.Menu();
                    break;
                case 4:
                    System.out.println("Write the account number you want to print ");
                    int g = scanner.nextInt();
                    for (int n = 0; n < b + 1; n++) {
                        if (accounts[n].AccountNumber == g) {
                            accounts[n].viewProperties();

                        }
                        else
                        {
                             System.out.println("This account number does not exist");

                        }

                    }
                    bank.Menu();
                    break;
                case 5:

                    for (int  f= 0; f< b + 1; f++) {

                            if(accounts[f]!=null)
                            {

                                System.out.println((f + 1) + " Account Number: " + accounts[f].AccountNumber + " Account Holder: " + accounts[f].AccountName + " Amount: " + accounts[f].Amount);


                            }





                    }

                    bank.Menu();
                    break;
                case 6:
                    bank.End();

                    ok = false;
                    break;


            }
            //BankAccount B = new BankAccount(AccountNum, AccountName,Amount);
           // accounts[numberOfAccounts] = B;
           // numberOfAccounts++;

        }


    }

    public static void main(String[] args) {
        // TODO code application logic here
        bankSystem bankSystem = new bankSystem();
        bankSystem.RunApplication();
    }
}

